<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="zh">
<head>
    <title>生成业务表</title>
    <jsp:include page="/WEB-INF/view/commons/css.jsp" />
    <style>
        .checkbox-inline{
            padding-left: 0px;
        }
    </style>
</head>
<body >
<div id="main-container" class="container-fluid">
    <form  action="gentable/${action eq "create" ?"save":"update"}" name="inputForm" id="inputForm" method="post" class="form-horizontal">
        <input name="id" id="id" value="${entity.id}" type="hidden" />


        <div class="form-group">
            <label class="col-sm-1 control-label">* 项目：</label>
            <div class="col-md-4">
                <d:select clazz="selectpicker" list="${genProjects}" name="projectId" selected="${entity.projectId}" require="true"></d:select>
            </div>
            <label class="col-sm-1 control-label">* 表名：</label>
            <div class="col-md-4">
                <d:select id="tableName" clazz="selectpicker" list="${tables}" name="tableName" selected="${entity.tableName}"></d:select>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-1 control-label">* 模块包名：</label>
            <div class="col-md-4">
                <input type="text" class="form-control" name="modulePackage" value="${entity.modulePackage}" placeholder="模块包名">
            </div>
            <label class="col-sm-1 control-label">模块名：</label>
            <div class="col-md-4">
                <input type="text" class="form-control" name="moduleName" value="${entity.moduleName}" placeholder="模块名">
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-1 control-label">* 功能名：</label>
            <div class="col-md-4">
                <input type="text" class="form-control" name="functionName" value="${entity.functionName}" placeholder="功能名">
            </div>
            <label class="col-sm-1 control-label">* 功能描述：</label>
            <div class="col-md-4">
                <input type="text" class="form-control" name="functionRemark" value="${entity.functionRemark}" placeholder="功能描述">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-1 control-label">* 类名：</label>
            <div class="col-md-4">
                <input type="text" class="form-control" name="className" value="${entity.className}" placeholder="类名">
            </div>
            <label class="col-sm-1 control-label">* 权限编码：</label>
            <div class="col-md-4">
                <input type="text" class="form-control" name="permissionCode" value="${entity.permissionCode}" placeholder="功能描述">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-1 control-label">* URL前缀：</label>
            <div class="col-md-4">
                <input type="text" class="form-control" name="urlPre" value="${entity.urlPre}" placeholder="URL前缀">
            </div>
            <label class="col-sm-1 control-label">* 代码版本：</label>
            <div class="col-md-4">
                <input type="text" class="form-control" name="codeVersion" value="${entity.codeVersion}" placeholder="代码版本">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-1 control-label">* 代码作者：</label>
            <div class="col-md-4">
                <input type="text" class="form-control" name="codeAuthor" value="${entity.codeAuthor}" placeholder="代码作者">
            </div>
            <label class="col-sm-1 control-label">* 列数：</label>
            <div class="col-md-4">
                <input type="text" class="form-control" name="columnNum" value="${entity.columnNum}" placeholder="列数">
            </div>
        </div>

        <table id="table_data" class="table table-striped table-condensed table-hover">
            <thead>
            <tr>
                <th width="5%">列名</th>
                <th width="10%">列描述</th>
                <th width="5%">字段类型</th>
                <th width="5%">jdbc类型</th>
                <th width="5%">java类型</th>
                <th width="10%">java字段名</th>
                <th width="15%">插入/必须/列表/查看/查询</th>
                <th width="5%">匹配方式</th>
                <th width="5%">表单类型</th>
                <th width="10%">字典类型</th>
                <th width="5%">排序</th>
            </tr>
            </thead>
            <tbody id="columnBody">
            </tbody>
        </table>

        <div class="form-group">
            <div class="col-sm-offset-1 col-sm-2">
                <button id="saveBtn" type="submit" class="btn btn-sm btn-primary"><i class="fa fa-floppy-o"></i> 提交</button>
                <button type="button" class="btn btn-sm link_close"><i class="fa fa-close"></i> 关闭</button>
            </div>
        </div>
    </form>

</div><!--/#main-container-->

<!-- script start -->
<jsp:include page="/WEB-INF/view/commons/js.jsp" />
<script type="text/javascript">
    $(document).ready(function(e) {
        var tableId = $('#id').val();
        if(tableId){
            $.ajax({
                url: "gentable/getTableInfoForEdit",
                type: "GET",
                dataType: "json",
                data:{tableId:tableId},
                success: function (res) {
                    if(res.success){
                        var map = res.data;
                        var table = map.table;
                        var columns = table.columns;
                        var html = template('columnTmp',{columns:columns,compareTypes:map.compareTypes, formTypes:map.formTypes});
                        $('#columnBody').html(html);
                        $( "input[name=dicType]" ).autocomplete({
                            source: map.dicTypes
                        });
                    }
                }
            });
        }


        var options = {
            rules:{
                projectId:{required:true},
                moduleName:{required:true},
                modulePackage:{required:true},
                tableName:{required:true},
                functionName:{required:true},
                columnNum:{required:true, digits : true},
                codeAuthor:{required:true},
                functionRemark:{required:true},
                codeVersion:{required:true}
            },
            submitHandler:function(form){

                $('#columnBody tr').each(function(i,item){
                    $(this).find('input, select').each(function(j){
                        var name = $(this).attr('name');
                        if(name.indexOf('.') > 0){
                            name = name.substring(name.indexOf('.') + 1);
                        }
                        $(this).attr('name', 'columns['+ i + '].'+ name);
                    });
                });

                $(form).ajaxSubmit({
                    type:'post',
                    dataType:'json',
                    success:function(data){
                        if(data.success){
                            $('#searchBtn', parent.document).click();
                            closeIframe();
                        }else{
                            layer.alert(data.msg);
                        }
                    }
                });
            }
        };
        $("#inputForm").validate(options);

        $('#tableName').change(function(){
            $.ajax({
                url: "gentable/getTableInfoFromDbSchema",
                type: "GET",
                dataType: "json",
                data:{tableName:this.value},
                success: function (res) {
                    if(res.success){
                        var map = res.data;
                        var table = map.table;
                        $('#inputForm input[type=text]').each(function(i, item){
                            if(table[this.name] != undefined){
                                this.value = table[this.name];
                            }
                        });
                        var columns = table.columns;
                        var html = template('columnTmp',{columns:columns,compareTypes:map.compareTypes, formTypes:map.formTypes});
                        $('#columnBody').html(html);
                        $( "input[name=dicType]" ).autocomplete({
                            source: map.dicTypes
                        });
                    }
                }
            });
        });
    });
</script>
<!-- script end -->
</body>
<script id="columnTmp" type="text/html">
    {{each columns item index}}
    <tr>
        <td>
            {{item.columnName}}
            <input type="hidden" class="form-control" name="columnName" value="{{item.columnName}}">
            <input type="hidden" class="form-control" name="isPk" value="{{item.isPk}}">
            <input type="hidden" class="form-control" name="columnType" value="{{item.columnType}}">
        </td>
        <td><input type="text" class="form-control" name="columnRemark" value="{{item.columnRemark}}"></td>
        <td>{{item.columnType}}</td>
        <td><input type="text" class="form-control" name="jdbcType" value="{{item.jdbcType}}"></td>
        <td><input type="text" class="form-control" name="javaType" value="{{item.javaType}}"></td>
        <td><input type="text" class="form-control" name="javaField" value="{{item.javaField}}"></td>
        <td>
            <label class="checkbox-inline">
                <input type="checkbox" class="ace" name="isInsert" {{if item.isInsert}} checked="checked" {{/if}} >
                <span class="lbl"> </span>
            </label>
            <label class="checkbox-inline">
                <input type="checkbox" class="ace" name="isRequire" {{if item.isRequire}} checked="checked" {{/if}}>
                <span class="lbl"> </span>
            </label>
            <label class="checkbox-inline">
                <input type="checkbox" class="ace" name="isList" {{if item.isList}} checked="checked" {{/if}} >
                <span class="lbl"> </span>
            </label>
            <label class="checkbox-inline">
                <input type="checkbox" class="ace" name="isInfo" {{if item.isInfo}} checked="checked" {{/if}} >
                <span class="lbl"> </span>
            </label>
            <label class="checkbox-inline">
                <input type="checkbox" class="ace" name="isQuery" {{if item.isQuery}} checked="checked" {{/if}} >
                <span class="lbl"> </span>
            </label>
        </td>
        <td>
            <select name="compareType">
                {{each compareTypes compareType}}
                <option value="{{compareType.value}}" {{if compareType.value==item.compareType }} selected="selected" {{/if}}>{{compareType.label}}</option>
                {{/each}}
            </select>
        </td>
        <td>
            <select name="formType">
                {{each formTypes formType}}
                <option value="{{formType.value}}" {{if formType.value==item.formType }} selected="selected" {{/if}}>{{formType.label}}</option>
                {{/each}}
            </select>
        </td>
        <td><input type="text" class="form-control" name="dicType" value="{{item.dicType}}"></td>
        <td><input type="text" class="form-control" name="sortNum" value="{{item.sortNum}}"></td>
    </tr>
    {{/each}}
</script>
</html>
